Python

How to create a currency convertor in python

How to create a currency convertor in python, someone asked me to explain?

In this tutorial I will show you how to create a currency convertor using python program.

Install required Package:

pip install python-dateutil requests BeautifulSoup4

PYTHON CODE:

import requests
from bs4 import BeautifulSoup as bs
import re
from dateutil.parser import parse

def convert_currency_xe(src, dst, amount):
    url = f"https://www.xe.com/currencyconverter/convert/?Amount={amount}&From={src}&To={dst}"
    content = requests.get(url).content
    soup = bs(content, "html.parser")
    exchange_rate_html = soup.find_all("p")[2]
    last_updated_datetime = parse(re.search(r"Last updated (.+)", exchange_rate_html.parent.parent.find_all("div")[-2].text).group()[12:])
    return last_updated_datetime, exchange_rate_html.text

if __name__ == "__main__":
    from_currency = input("From Currency: ").upper()
    to_currency = input("To Currency: ").upper()
    amount = int(input("Enter the amount: "))
    
    last_updated_datetime, exchange_rate = convert_currency_xe(from_currency, to_currency, amount)
    print("Last updated datetime:", last_updated_datetime)
    print(f"{amount} {from_currency} = {exchange_rate} {to_currency}")

OUTPUT:

Currency convertor using python

VIDEO GUIDE:

Post your comments / questions